package com.candykingdom.components
{
	import com.candykingdom.base.Component;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	[Event(name="complete", type="flash.events.Event")]
	
	public class Image extends Component
	{
		private var _source:Object;
		private var _bitmap:Bitmap;
		private var _loader:Loader;
		private var _loading:Boolean;
		
		public function Image()
		{
			super();
		}
		
		public function setSize(wid:Number ,hig:Number):void
		{
			graphics.clear();
			graphics.beginFill(0,0);
			graphics.drawRect(0 ,0 ,wid ,hig);
			graphics.endFill();
		}
		
		
		/**
		 * 源 绑定的Class 或者BitmapData 或者图片路径
		 * @param value
		 * 
		 */		
		public function set source(value:Object):void
		{
			if(value is Class)
			{
				var bmp:Bitmap = new value() as Bitmap;
				_bitmap = new Bitmap(createBitmap(bmp) ,"auto" ,true);
				addChild(_bitmap);
				bmp.bitmapData.dispose();
				bmp = null;
			}
			
			if(value is BitmapData)
			{
				_bitmap = new Bitmap(value as BitmapData ,"auto" ,true);
				addChild(_bitmap);
				return;
			}
			
			if(value is String)
			{
				loadHandler(value.toString());
				return;
			}
		}
		
		
		
		/**
		 * bitmapData 
		 * @return 
		 * 
		 */		
		public function get bitmapData():BitmapData
		{
			if(_bitmap!=null)return _bitmap.bitmapData;
			return null;
		}
		
		
		/**
		 * 回收 
		 * 
		 */		
		public function dispose():void
		{
			if(_loader!=null)
			{
				if(_loading)_loader.close();
				_loader.unloadAndStop();
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE ,loadComplete);
				_loader = null;
			}
			
			if(_bitmap!=null)
			{
				removeChild(_bitmap);
				_bitmap.bitmapData.dispose();
				_bitmap = null;
			}
			
			_loading = false;
		}
		
		
		
		
		
		
		/**
		 * 加载处理 
		 * @param path
		 * 
		 */		
		private function loadHandler(path:String):void
		{
			dispose();
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,loadComplete);
			_loader.load(new URLRequest(path));
			_loading = true;
		}
		
		private function loadComplete(event:Event):void
		{
			_loading = false;
			_bitmap = new Bitmap(createBitmap(_loader) ,"auto" ,true);
			addChild(_bitmap);
			
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE ,loadComplete);
			_loader.unloadAndStop();
			_loader = null;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		
		/**
		 * 创建位图 
		 * @param display
		 * @return 
		 * 
		 */		
		private function createBitmap(display:DisplayObject):BitmapData
		{
			var bd:BitmapData = new BitmapData(display.width ,display.height ,true ,0x00);
			var matrix:Matrix = new Matrix();
			matrix.a = width/(display.width*transform.matrix.a);
			matrix.d = height/(display.height*transform.matrix.d);
			bd.draw(display ,matrix ,null ,null ,null ,true);
			matrix = null;
			return bd;
		}
	}
}